package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.MenuPathBo;
import com.tpyule.tpclass.entity.bo.PathBo;
import com.tpyule.tpclass.entity.bo.PathDetailBo;
import com.tpyule.tpclass.entity.dto.PathQueryDto;
import com.tpyule.tpclass.entity.dto.PathSelDto;
import com.tpyule.tpclass.entity.pojo.Path;
import com.tpyule.tpclass.entity.pojo.PathGroup;
import com.tpyule.tpclass.service.PathService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/24 21:03
 */
@RestController
@RequestMapping("/path")
public class PathController extends BaseController{
    @Autowired
    PathService pathService;

    @RequestMapping("/getSizePath")
    @ResponseBody
    public void getSizePath(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryData = request.getParameter("queryData");
            if(queryData!=null){
                PathQueryDto dto = objectMapper.readValue(queryData, PathQueryDto.class);
                int result = pathService.getSizePath(dto);
                if(result!=0){
                    resultBean = new SuccessResultBean(result, "成功！");
                }else{
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }

        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/getListGroup")
    @ResponseBody
    public void getListGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            List<PathGroup> groups = pathService.getListGroup();
            if (groups!=null){
                resultBean = new SuccessResultBean(groups, "成功！");
            } else {
                resultBean = new FailResultBean("失败");
            }
        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getListPath")
    @ResponseBody
    public void getListPath(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryData = request.getParameter("queryData");
            if(queryData!=null){
                PathQueryDto dto = objectMapper.readValue(queryData, PathQueryDto.class);
                List<PathDetailBo> paths = pathService.getListPath(dto);
                if (paths!=null){
                    resultBean = new SuccessResultBean(paths, "成功！");
                } else {
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/addPath")
    @ResponseBody
    public void addPath(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String pathData = request.getParameter("pathData");
            if(pathData!=null){
                Path path = objectMapper.readValue(pathData, Path.class);
                int result = pathService.addPath(path);
                if (result!=0){
                    resultBean = new SuccessResultBean( "成功！");
                } else {
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/updatePath")
    @ResponseBody
    public void updatePath(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String pathData = request.getParameter("pathData");
            if(pathData!=null){
                Path path = objectMapper.readValue(pathData, Path.class);
                int result = pathService.updatePath(path);
                if (result!=0){
                    resultBean = new SuccessResultBean( "成功！");
                } else {
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/deletePath")
    @ResponseBody
    public void deletePath(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String pathData = request.getParameter("pathData");
            if(pathData!=null){
                Path path = objectMapper.readValue(pathData, Path.class);
                int result = pathService.deletePath(path);
                if (result!=0){
                    resultBean = new SuccessResultBean( "成功！");
                } else {
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getAllPath")
    @ResponseBody
    public void getAllPath(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            List<Path> paths = pathService.getAllPath();
            if (paths!=null){
                resultBean = new SuccessResultBean(paths, "成功！");
            } else {
                resultBean = new FailResultBean("失败");
            }
        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getAllMenu")
    @ResponseBody
    public void getAllMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryData = request.getParameter("queryData");
            if(queryData!=null){
                PathSelDto dto = objectMapper.readValue(queryData, PathSelDto.class);
                List<PathBo> paths = pathService.getMenu(dto);
                if (paths!=null){
                    resultBean = new SuccessResultBean(paths, "成功！");
                } else {
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }


    @RequestMapping("/getMenuPath")
    @ResponseBody
    public void getMenuPath(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userId = request.getParameter("userId");
            String type = request.getParameter("type");
            if(userId!=null&&type!=null){
                int t = Integer.parseInt(type);
                List<MenuPathBo> bos = pathService.getMenuPath(userId,t);
                if (bos!=null){
                    resultBean = new SuccessResultBean(bos, "成功！");
                } else {
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        }finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

}
